package com.fowo.api.costAccounting.service.impl;

import com.fowo.api.costAccounting.entity.CarryFcorwardConfig;
import com.fowo.api.costAccounting.mapper.CommonCostingMapper;
import com.fowo.api.costAccounting.model.bd.BdAccperiodmonthVo;
import com.fowo.api.costAccounting.model.carry.CarryFcorwardConfigProductVo;
import com.fowo.api.costAccounting.model.stock.StockVo;
import com.fowo.api.costAccounting.service.CarryFcorwardConfigProductService;
import com.fowo.api.costAccounting.service.CarryFcorwardConfigService;
import com.fowo.api.costAccounting.service.CarryFcorwardBusinessService;
import com.fowo.api.costAccounting.service.MonthlyCostReportService;
import com.fowo.api.costAccounting.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CarryFcorwardBusinessServiceImpl implements CarryFcorwardBusinessService {
    @Resource
    protected CarryFcorwardConfigService carryFcorwardConfigService;
    @Resource
    protected CarryFcorwardConfigProductService carryFcorwardConfigProductService;
    @Resource
    private CommonCostingMapper commonCostingMapper;
    @Resource
    protected MonthlyCostReportService monthlyCostReportService;
    @Override
    public void updateMonthlyCostReport(String serialNumber) throws Exception {
        CarryFcorwardConfig carryFcorwardConfig = carryFcorwardConfigService.getSerialNumber(serialNumber);
        List<CarryFcorwardConfigProductVo> carryFcorwardConfigProductVoList= carryFcorwardConfigProductService.getByCarryFcorwardConfigId(carryFcorwardConfig.getId());
        if(carryFcorwardConfigProductVoList.size()==0){
            throw new Exception("结转子表配置查出没有符合得数据");
        }
        List<BdAccperiodmonthVo> bdAccperiodmonthVoList = commonCostingMapper.getByStatus();
        if (bdAccperiodmonthVoList.size() != 1) {
            throw new Exception("会计月份查出多条或没有符合得数据");
        }
        if (bdAccperiodmonthVoList.get(0).getBeginDate() == null || bdAccperiodmonthVoList.get(0).getEndDate() == null) {
            throw new Exception("会计月份开始时间结束日期未设置");
        }
        //获取当月 处理获取上个月配置的日期查询出
        String ym= bdAccperiodmonthVoList.get(0).getAccperiodMonth();
        if(StringUtils.isBlank(ym)){
            throw new Exception("会计月份未配置");
        }
        //上个月的
        List<BdAccperiodmonthVo> lastbdAccperiodmonthVoList = commonCostingMapper.getByAccperiodMonth(DateUtils.getLastMonth(ym));
        if (lastbdAccperiodmonthVoList.size() != 1) {
            throw new Exception("上个月会计月份查出多条或没有符合得数据");
        }
        if (lastbdAccperiodmonthVoList.get(0).getBeginDate() == null || lastbdAccperiodmonthVoList.get(0).getEndDate() == null) {
            throw new Exception("上个会计月份开始时间结束日期未设置");
        }
        this.thread(carryFcorwardConfig,carryFcorwardConfigProductVoList,bdAccperiodmonthVoList.get(0),lastbdAccperiodmonthVoList.get(0));
    }

    /**
     *
     * @param carryFcorwardConfig
     * @param carryFcorwardConfigProductVoList
     * @param nowBdAccperiodmonthVo
     * @param lastBdAccperiodmonthVo
     */
    public void thread(CarryFcorwardConfig carryFcorwardConfig,List<CarryFcorwardConfigProductVo> carryFcorwardConfigProductVoList,BdAccperiodmonthVo nowBdAccperiodmonthVo,BdAccperiodmonthVo lastBdAccperiodmonthVo) throws Exception{

        //入库数据
        List<StockVo> in=new ArrayList<>();
        //出库数据
        List<StockVo> out=new ArrayList<>();
        for (CarryFcorwardConfigProductVo carryFcorwardConfigProductVo : carryFcorwardConfigProductVoList) {
             String sql=String.format(carryFcorwardConfigProductVo.getSelectSql(),DateUtils.getTimesBeginStr(lastBdAccperiodmonthVo.getBeginDate()),DateUtils.getTimesEndStr(lastBdAccperiodmonthVo.getEndDate()));
            List<StockVo> stockVosList= commonCostingMapper.selectSqlByCarrForward(sql);
            if(stockVosList.size()>0){
                if(1==carryFcorwardConfigProductVo.getType()){
                    in.addAll(stockVosList);
                }else if(2==carryFcorwardConfigProductVo.getType()){
                    out.addAll(stockVosList);
                }
            }
        }
        // 入库直接 添加 出库比较 是添加还是修改
         monthlyCostReportService.saveInBatch(in,nowBdAccperiodmonthVo.getId());
         List<StockVo> addList=this.stockVoInAdd(in,out,carryFcorwardConfig.getType());
        // List<StockVo> updateList=this.stockVoInUpdate(in,out,carryFcorwardConfig.getType());
        //不过滤出库得 直接根据条件修改 没有的通过添加
         monthlyCostReportService.updateOutBatch(out,nowBdAccperiodmonthVo.getId(),carryFcorwardConfig.getType());
         monthlyCostReportService.saveOutBatch(addList,nowBdAccperiodmonthVo.getId());
         //本月得 入库出库添加
         //copy到库存余额结转
         monthlyCostReportService.insertCopys(nowBdAccperiodmonthVo.getId());
    }

    /**
     *   添加的数据
     * @param in
     * @param out
     * @param type 据维度(1 - 组织+物料; 2 - 组织+仓库+物料; 3 - 组织+店铺+物料; )
     */
    public List<StockVo> stockVoInAdd(List<StockVo> in,List<StockVo> out,Integer type){
        List<StockVo> listadd=new ArrayList<>();
        if(type==1){
            listadd =
                    out.stream().filter(item -> !in.stream().map(up -> up.getItemId()+up.getOrgId()).collect(
                            Collectors.toList()).contains(item.getItemId()+item.getOrgId())).collect(Collectors.toList());
            return listadd;
        }else if(type==2){
            listadd =
                    out.stream().filter(item -> !in.stream().map(up -> up.getItemId()+up.getStockId()+up.getOrgId()).collect(
                            Collectors.toList()).contains(item.getItemId()+item.getStockId()+item.getOrgId())).collect(Collectors.toList());
            return listadd;
        }else if(type==3){
            listadd =
                    out.stream().filter(item -> !in.stream().map(up -> up.getItemId()+up.getOrgId()+up.getShopId()).collect(
                            Collectors.toList()).contains(item.getItemId()+item.getOrgId()+item.getShopId())).collect(Collectors.toList());
            return listadd;
        }
        return listadd;
    }
    /**
     *   修改的数据
     * @param in
     * @param out
     * @param type 据维度(1 - 组织+物料; 2 - 组织+仓库+物料; 3 - 组织+店铺+物料; )
     */
    public List<StockVo> stockVoInUpdate(List<StockVo> in,List<StockVo> out,Integer type){
        List<StockVo> listadd=new ArrayList<>();
        if(type==1){
            listadd =
                    out.stream().filter(item -> in.stream().map(up -> up.getItemId()+up.getOrgId()).collect(
                            Collectors.toList()).contains(item.getItemId()+item.getOrgId())).collect(Collectors.toList());
            return listadd;
        }else if(type==2){
            listadd =
                    out.stream().filter(item -> in.stream().map(up -> up.getItemId()+up.getStockId()+up.getOrgId()).collect(
                            Collectors.toList()).contains(item.getItemId()+item.getStockId()+item.getOrgId())).collect(Collectors.toList());
            return listadd;
        }else if(type==3){
            listadd =
                    out.stream().filter(item -> in.stream().map(up -> up.getItemId()+up.getOrgId()+up.getShopId()).collect(
                            Collectors.toList()).contains(item.getItemId()+item.getOrgId()+item.getShopId())).collect(Collectors.toList());
            return listadd;
        }
        return listadd;
    }
}
